package com.aiju.cash.ui.multiple_shops.activity;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Shop;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.ShopManagetApi;
import com.aiju.cash.core.event.ClerkAddEvent;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.T;
import com.aiju.utils.Util;
import com.aiju.widgit.dialog.ListDialog;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-13 10:03
 * @version: V1.0
 * @Description: 添加店员
 */
public class ClerkAddActivity extends BaseTitleActivity {

    public static final int SHOW_ADD = 0; //添加
    public static final int SHOW_INFO = 1; //信息
    public static List<String> DATA = new ArrayList<>();

    static {
        DATA.add("店长");
        DATA.add("店员");
    }

    private SettingItemEditView nameEditView, phoneEditView;
    private SettingItemTextView shopTextView, typeTextView, stateTextView;

    private Shop mShop;
    private ShopClerk mShopClerk;

    private int showType;
    private String role, userName, phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_clerk);
        setCenterTitle("添加店员");
        showType = getIntent().getIntExtra("showType", 0);

        phoneEditView = (SettingItemEditView) findViewById(R.id.set_clerk_phone);
        phoneEditView.getleftView().setText(Html.fromHtml("手机号<font color='#ec4545'>*</font>"));
        phoneEditView.getRightView().setHint("手机号将作为店员的登录账号");
        phoneEditView.setEditable(true);

        nameEditView = (SettingItemEditView) findViewById(R.id.set_clerk_name);
        nameEditView.getRightView().setHint("默认为店员的手机号");
        nameEditView.setEditable(true);

        shopTextView = (SettingItemTextView) findViewById(R.id.set_clerk_shop);
        shopTextView.getleftView().setText(Html.fromHtml("所属门店<font color='#ec4545'>*</font>"));
        typeTextView = (SettingItemTextView) findViewById(R.id.set_clerk_type);
        typeTextView.getleftView().setText(Html.fromHtml("身份<font color='#ec4545'>*</font>"));
        typeTextView.setRightText("店长");
        typeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showType == SHOW_INFO && mShopClerk.getRole().equals("0")) {
                    T.ToastShort("你是总店长，不能修改身份");
                    return;
                }
                new ListDialog(ClerkAddActivity.this)
                        .setData(DATA)
                        .setSelectString(typeTextView.getRightString())
                        .setLayoutParams()
                        .setOnItemOnClick(new ListDialog.OnItemOnClick() {
                            @Override
                            public void click(String item, int position) {
                                typeTextView.setRightText(item);
                            }
                        })
                        .show();
            }
        });
        stateTextView = (SettingItemTextView) findViewById(R.id.set_clerk_state);

        mShop = (Shop) getIntent().getSerializableExtra("shop");
        shopTextView.setRightText(mShop.getShopName());
        if (showType == SHOW_ADD) {
            stateTextView.setVisibility(View.GONE);
            findViewById(R.id.btn_sent_message).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    savaClerk();
                }
            });
        } else {
            stateTextView.setVisibility(View.VISIBLE);
            phoneEditView.setEditable(false);
            mShopClerk = (ShopClerk) getIntent().getSerializableExtra("shopClerk");
            showShopClerk(mShopClerk);
            findViewById(R.id.btn_sent_message).setVisibility(View.GONE);
            mAiJuTitleBarView.setRightTitle("保存");
            mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateClerk();
                }
            });
        }
    }

    /**
     * 更改店员信息
     */
    private void updateClerk() {
        String type = typeTextView.getRightString();
        if (type.equals("总店长")) {
            role = "0";
        } else {
            role = type.equals("店长") ? "1" : "2";
        }
        userName = nameEditView.getRightText();
        new ShopManagetApi(mDialogHelper).updateClerk(mShop.getStoreId(), mShopClerk.getId(), userName, role, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("成功修改店员信息");
                EventCenter.getInstance().post(new ClerkAddEvent());
                finish();
            }
        });
    }

    /**
     * 显示店员信息
     *
     * @param shopClerk
     */
    private void showShopClerk(ShopClerk shopClerk) {
        phoneEditView.setRightText(shopClerk.getPhone());
        ((EditText) phoneEditView.getRightView()).setSelection(shopClerk.getPhone().length());
        nameEditView.setRightText(shopClerk.getUserName());
        ((EditText) nameEditView.getRightView()).setSelection(shopClerk.getUserName().length());
        if (shopClerk.getIsActivate().equals("0")) {
            stateTextView.getRightView().setTextColor(Util.getColor(R.color.color_999999));
            stateTextView.setRightText("未激活");
        } else {
            stateTextView.getRightView().setTextColor(Util.getColor(R.color.app_display_color));
            stateTextView.setRightText("已激活");
        }
        typeTextView.setRightText(shopClerk.getRoleString());
    }

    /**
     * 添加店员
     */
    private void savaClerk() {
        phone = phoneEditView.getRightText();
        if (!CheckUtil.checkPhone(phone)) {
            T.ToastShort("请输入正确的手机号");
            return;
        }
        userName = nameEditView.getRightText();
        if (!TextUtils.isEmpty(userName)) {
            if (!CheckUtil.checkMemberCardName(userName)) {
                T.ToastShort("店员姓名仅支持不超过10个字符的汉字、数字和英文");
                return;
            }
        } else {
            userName = phone;
        }

        String type = typeTextView.getRightString();
        if (TextUtils.isEmpty(type)) {
            T.ToastShort("请设置店员身份");
            return;
        }
        role = type.equals("店长") ? "1" : "2";

        new ShopManagetApi(mDialogHelper).addClerk(mShop.getStoreId(), mShop.getId(), mShop.getShopName(),
                mShop.getParentStoreId(), role, userName, phone, mUser.getId(), mUser.getPhone(), new SimpleHttpCallback() {
                    @Override
                    protected void onDataSuccess(String data) {
                        T.ToastShort("成功添加店员");
                        EventCenter.getInstance().post(new ClerkAddEvent());
                        finish();
                    }
                });
    }
}
